import copy
import datetime
from time import sleep
from selenium import webdriver
import requests
from selenium.webdriver.common.by import By
import selenium.common.exceptions
import login_info.login_info as login
from selenium import webdriver
from tools.sim_land import conn
import tools.make_string as table_name
import tools.table_classify as tb
import tools.log_op as write_log
class river_table_info_2():
    def __init__(self):
        self.emr_user = login.emr_user
        self.emr_password = login.emr_password
        self.get_cookie_url = login.river_get_cookie_url
        chrome_options = webdriver.ChromeOptions()
        self.browser = webdriver.Chrome(options=chrome_options)
        self.land_tool = conn(self.browser)

    # 登录river
    def studio_land(self):
        land_tool = self.land_tool
        land_tool.url_conn(self.get_cookie_url)
        # 获取input
        login_input = land_tool.find_by_id('login-by')
        login_input.send_keys(self.emr_user)
        # 密码input
        login_input = land_tool.find_by_id('password')
        login_input.send_keys(self.emr_password)
        # 点击click按键
        button = land_tool.find_by_id('btn-submit-login')
        button.click()
        dtalis = land_tool.find_by_id('details-button')
        dtalis.click()
        go = land_tool.find_by_id('proceed-link')
        go.click()
        sleep(10)

    # 点击工作空间
    def go_work_space(self):
        land_tool = self.land_tool
        work_space = land_tool.find_by_xpath('//*[@id="root"]/div/div/div/div/div[2]/div[2]/div/div/div/div[4]/div')
        work_space.click()

    # 点击数据地图
    def go_data_map(self):
        land_tool = self.land_tool
        map = land_tool.find_by_text('数据地图')
        map.click()

    # 填充搜索框，并点击
    def fill_search_bar(self, data_table):
        land_tool = self.land_tool
        search_bar = land_tool.find_by_xpath(
            '//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div/div/div[1]/span[1]/input')
        search_bar.send_keys(data_table)
        ok_button = land_tool.find_by_xpath(
            '//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div/div/div[1]/span[1]/span/button')
        ok_button.click()

    # 进入详情界面
    def href(self, data_table):
        land_tool = self.land_tool
        href = land_tool.find_by_text(data_table)
        href.click()
        # 进入标准清洗界面
        land_tool = self.land_tool
        std_index = land_tool.find_by_xpath(
            '//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[5]')
        std_index.click()

    # 填充值
    def fill_value(self,data_table=''):


        land_tool = self.land_tool
        #节点名称
        job_name = land_tool.find_by_xpath('//*[@id="job_name"]')
        job_name.send_keys(table_name.node_name(data_table))
        #下拉箭头
        select_arrow = land_tool.find_by_xpath('//*[@id="exceptionRules"]/div[1]/span')
        select_arrow.click()
        #/html/body/div/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/form/div[2]/div[3]/div/div[2]/div/span/div/div[2]/div/div/div/ul/li[2]
        #输出到问题表

        sleep(2)
        #这行容易出现问题
        que_table = land_tool.find_by_xpath('/html/body/div/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/form/div[2]/div[3]/div/div[2]/div/span/div/div[2]/div/div/div/ul/li[2]')
        que_table.click()
        #目的表原表克隆，填充值
        aim_clone_click = land_tool.find_by_xpath('/html/body/div/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/form/div[2]/div[2]/div[2]/div')
        aim_clone_click.click()
        #目的表input
        aim_input = land_tool.find_by_xpath('/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div/div/div[1]/form/div[1]/div[1]/div/div[2]/div/span/input')
        aim_input.clear()
        aim_input.send_keys(table_name.std_name(data_table))
        #aim confirm
        aim_confirm = land_tool.find_by_xpath('/html/body/div[2]/div/div[2]/div/div[2]/div[3]/div/button[2]')
        aim_confirm.click()
        sleep(10)
        land_tool.find_by_xpath_click('//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/form/div[3]/div[2]/div[2]/div')
        #clone 表
        clone_input = land_tool.find_by_xpath(
            '/html/body/div[2]/div/div[2]/div/div[2]/div[2]/div/div/div/div[1]/form/div[1]/div[1]/div/div[2]/div/span/input')
        clone_input.clear()
        clone_input.send_keys(table_name.err_name(data_table))
        land_tool.find_by_xpath('/html/body/div[2]/div/div[2]/div/div[2]/div[3]/div/button[2]').click()
        sleep(6)
        # land_tool.find_by_xpath('//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/div[3]/div[2]/div[2]/div[1]/div/label/span/input')
        # 'div[class*="loadingWhiteBox"]'
        # cloneButton___22btn
    #基础清洗下拉框
    def click_basic_arrow(self):
        land_tool = self.land_tool
        c = land_tool.find_by_xpath('/html/body/div/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/div[3]/div[2]/div[2]/div')
        c.click()


    def click_basic_check_box_by_class(self):
        browser = self.browser
        list = browser.find_elements(By.CLASS_NAME,'')
        print(1)
    #选择去重
    def click_distinct_arrow(self):
        land_tool = self.land_tool
        c = land_tool.find_by_xpath('//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/div[3]/div[2]/div[4]/div[1]/i')
        c.click()

        # basic_win

    def basic_win(self):
        flag = 0
        start = 0
        self.end = 0
        browser = self.browser
        inputs = browser.find_elements_by_tag_name('input')
        for index, input in enumerate(inputs):
            if (input.get_attribute('type') == 'checkbox' and flag == 0):
                start = index
                flag = flag +1
        for index, input in enumerate(inputs):
            # 容易出现问题？？
            if (input.get_attribute('type') == 'checkbox' ):
                self.end = index
        print(start)
        sleep(1)
        for index, input in enumerate(inputs):
            # 容易出现问题？？
            if (input.get_attribute('type') == 'checkbox' and (index == start or index == start+5)):
                print('click')
                input.click()
    #去重勾选
    def distinct_win(self):
        flag = 0
        start = 0
        end = 0
        browser = self.browser
        inputs = browser.find_elements_by_tag_name('input')

        for index, input in enumerate(inputs):
            #  and (index == 5 or index == 12)
            if (input.get_attribute('type') == 'checkbox' and (index == self.end or index == self.end+4)):
                input.click()
                print('click')

    def commit(self):
        land_tool = self.land_tool
        land_tool.find_by_xpath_click('//*[@id="root"]/div/div/div[2]/div[3]/div/div/div[1]/div/div[3]/div/div/div[2]/div[3]/div[5]/div[2]/div/div/div[1]/div[2]/button')
    def floder(self):
        land_tool = self.land_tool
        land_tool.find_by_xpath_click('//*[@id="40b0215a-5a2e-4032-a477-dea4414b37ec"]/span')
        sleep(3)
        land_tool.find_by_xpath_click('/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div/button[2]')

    def clean_process(self, data_table):
        try:
            self.studio_land()
            self.go_work_space()
            sleep(6)
            self.go_data_map()
            sleep(3)
            self.fill_search_bar(data_table)
            sleep(5)
            self.href(data_table)
            sleep(3)
            self.click_basic_arrow()
            sleep(1)
            self.basic_win()
            self.click_basic_arrow()
            sleep(1)
            self.click_distinct_arrow()
            sleep(1)
            self.distinct_win()
            self.click_distinct_arrow()
            sleep(5)
            self.fill_value(data_table)
            sleep(3)
            self.commit()
            self.floder()
            print('end')
            write_log.write_empty_success('清洗成功: '+data_table)
        except:
            write_log.write_empty_fail('清洗失败: '+data_table)
    def clean_cycle(self):
        list = tb.get_empty_table()
        for x in list:
            self.clean_process(x)
            sleep(3)
if __name__ == '__main__':
    a = river_table_info_2()
    a.clean_cycle()
